using System;
using System.Security.Cryptography;

namespace Wantn.Share.DataCry
{
	public sealed class DES
	{
		public static bool GenKey(out byte[] rgbKey, out byte[] rgbIV)
		{
			bool flag = false;
			DESCryptoServiceProvider dESCryptoServiceProvider = null;
			try
			{
				dESCryptoServiceProvider = new DESCryptoServiceProvider();
				dESCryptoServiceProvider.Mode = CipherMode.CBC;
				dESCryptoServiceProvider.Padding = PaddingMode.PKCS7;
				dESCryptoServiceProvider.GenerateKey();
				dESCryptoServiceProvider.GenerateIV();
				rgbKey = dESCryptoServiceProvider.Key;
				rgbIV = dESCryptoServiceProvider.IV;
				return true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				dESCryptoServiceProvider?.Clear();
			}
		}

		public static byte[] Encrypt(byte[] data, byte[] rgbKey, byte[] rgbIV)
		{
			byte[] array = null;
			DESCryptoServiceProvider dESCryptoServiceProvider = null;
			ICryptoTransform cryptoTransform = null;
			try
			{
				dESCryptoServiceProvider = new DESCryptoServiceProvider();
				dESCryptoServiceProvider.Mode = CipherMode.CBC;
				dESCryptoServiceProvider.Padding = PaddingMode.PKCS7;
				cryptoTransform = dESCryptoServiceProvider.CreateEncryptor(rgbKey, rgbIV);
				return cryptoTransform.TransformFinalBlock(data, 0, data.Length);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				cryptoTransform?.Dispose();
				dESCryptoServiceProvider?.Clear();
			}
		}

		public static byte[] Encrypt(byte[] data, out byte[] rgbKey, out byte[] rgbIV)
		{
			byte[] array = null;
			DESCryptoServiceProvider dESCryptoServiceProvider = null;
			ICryptoTransform cryptoTransform = null;
			try
			{
				dESCryptoServiceProvider = new DESCryptoServiceProvider();
				dESCryptoServiceProvider.Mode = CipherMode.CBC;
				dESCryptoServiceProvider.Padding = PaddingMode.PKCS7;
				dESCryptoServiceProvider.GenerateKey();
				dESCryptoServiceProvider.GenerateIV();
				rgbKey = dESCryptoServiceProvider.Key;
				rgbIV = dESCryptoServiceProvider.IV;
				cryptoTransform = dESCryptoServiceProvider.CreateEncryptor(rgbKey, rgbIV);
				return cryptoTransform.TransformFinalBlock(data, 0, data.Length);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				cryptoTransform?.Dispose();
				dESCryptoServiceProvider?.Clear();
			}
		}

		public static byte[] Decrypt(byte[] data, byte[] rgbKey, byte[] rgbIV)
		{
			byte[] array = null;
			DESCryptoServiceProvider dESCryptoServiceProvider = null;
			ICryptoTransform cryptoTransform = null;
			try
			{
				dESCryptoServiceProvider = new DESCryptoServiceProvider();
				dESCryptoServiceProvider.Mode = CipherMode.CBC;
				dESCryptoServiceProvider.Padding = PaddingMode.PKCS7;
				cryptoTransform = dESCryptoServiceProvider.CreateDecryptor(rgbKey, rgbIV);
				return cryptoTransform.TransformFinalBlock(data, 0, data.Length);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				cryptoTransform?.Dispose();
				dESCryptoServiceProvider?.Clear();
			}
		}

	}
}
